#!/usr/bin/bash -x
umask 2
CWSNAME=$1
BUILDER=$2
BUILDNO=$3
if [ -f ../../site_config ]; then . ../../site_config ; fi

cd ..
echo "Building..."
. ./solar.env

#gh debug hack
ls -l $SHARED_SOLARVERSION/$OUTPATH/inc/300minor.mk

# hack ause
# hack GH wieder raus weils sonst in toolkit bricht
#export WITH_LANG="en-US de"

if [ "$SOURCE_ROOT_DIR" ] ; then
  INSTSETOO=$SOURCE_ROOT_DIR/ooo/instsetoo_native
  INSTSET=$SOURCE_ROOT_DIR/sun/instset_native
else
  INSTSETOO=$SRC_ROOT/instsetoo_native
  INSTSET=$SRC_ROOT/instset_native
fi

cd $INSTSETOO && $PERL $SOLARENV/bin/build.pl --all $custom_buildflags
RETVAL=$?
if [ $RETVAL -eq 0 ]; then
  $PERL $SOLARENV/bin/deliver.pl
  RETVAL=$?
  if [ $RETVAL -eq 0 ]; then
    cd $INSTSET && $PERL $SOLARENV/bin/build.pl --all $custom_buildflags
    RETVAL=$?
    if [ $RETVAL -eq 0 ]; then
      $PERL $SOLARENV/bin/deliver.pl
      RETVAL=$?
    fi
  fi
fi

# Sending mail to CWS owner
STATUS="[UNKNOWN]"
if [ $RETVAL -eq 0 ]; then
  STATUS="[SUCCESS]";
else
  STATUS="[FAILED]";
fi

echo "Little test:"
cws query owner | tail -1
echo "Little test:"
cws query owner

USERNAME=$(cws query owner | tail -1)
MACHINE=$(uname -a)
SUBJECT="StarOffice Buildbot: build of $CWSNAME finished with status $STATUS"
echo "$MACHINE ($BUILDER) has finished build of your CWS $CWSNAME with status $STATUS" > mail.tmp
echo "" >> mail.tmp
echo "Visit http://so-berlin:8080/builders/$BUILDER/builds/$BUILDNO for output logs." >> mail.tmp
echo "" >> mail.tmp
echo "If you discover any problems with the Buildbot machines contact Team Tools & RelEng or use this mailalias: buildbot_interest@sun.com" >> mail.tmp
echo "" >> mail.tmp
echo "Thank you for using our StarOffice Buildbots :-)" >> mail.tmp

if [ "" != "$USERNAME" ]; then
  mail -s "$SUBJECT" $USERNAME < mail.tmp
else
  echo "cws query for email address failed!"
fi

#gh debug hack
ls -l $SHARED_SOLARVERSION/$OUTPATH/inc/300minor.mk

exit $RETVAL
